<?php

namespace App\Http\Controllers\Common;

use App\Helpers\Tree;
use App\Http\Controllers\Controller as BaseContoller;
use App\Http\Requests\SysMapRequest;
use App\Http\Responses\CustomResponse;
use App\Services\common\SysMapService;
use Illuminate\Http\Request;

/**
 * 字典管理
 */
class MapController extends BaseContoller
{
    /**
     * 列表
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function index(Request $request)
    {
        $list = SysMapService::getData($request->all());
        // 构建菜单树
        Tree::init($list, 'parent_val', 'id', 'map_name');
        $tree = Tree::get_childall_data();

        return CustomResponse::success($tree);
    }

    /**
     * 获取顶级列表
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function getSyscom(Request $request)
    {
        $list = SysMapService::getData([
            'parent_val' => 0,
        ]);
        $returnArray = [];
        // 递归函数，用于构建菜单层次结构
        function buildMenu($items, $parent_id = 0, $prefix = '')
        {
            $result = [];
            $totalItems = count($items);
            foreach ($items as $index => $item) {
                $label = ($index === $totalItems - 1) ? $prefix.'└ '.$item->map_name : $prefix.'├ '.$item->map_name;
                if ($item->parent_code == $parent_id) {
                    $result[] = [
                        'label' => $label,
                        'value' => $item->id,
                    ];
                    // 递归调用，为下级菜单项添加适当的前缀
                    $children = buildMenu($items, $item->id, $prefix.'│ ');
                    $result = array_merge($result, $children);
                }
            }

            return $result;
        }

        $returnArray = buildMenu($list);
        array_unshift($returnArray, ['label' => '顶级', 'value' => 0]);

        return CustomResponse::success($returnArray);
    }

    /**
     * 添加
     *
     * @param  Request  $request
     * @return void
     */
    public function store(SysMapRequest $request)
    {
        if ($request->input('id')) {
            // 编辑
            $requestAll = $request->all(['id', 'map_code', 'parent_val', 'map_name', 'map_val', 'map_val2', 'map_val3', 'sort', 'map_desc']);
            $id = SysMapService::edit($requestAll['id'], $requestAll);
            if ($id) {
                return CustomResponse::success($id, '编辑成功');
            } else {
                return CustomResponse::error('编辑失败');
            }
        } else {
            // 创建
            $requestAll = $request->all(['map_code', 'parent_val', 'map_name', 'map_val', 'map_val2', 'map_val3', 'sort', 'map_desc']);
            $result = SysMapService::create($requestAll);
            if ($result) {
                return CustomResponse::success($result->id, '创建成功');
            } else {
                return CustomResponse::error('创建失败');
            }
        }
    }

    /**
     * 删除
     *
     * @return void
     */
    public function destroy(Request $request, $id)
    {
        $parentValList = SysMapService::getData([
            'parent_val' => $id,
        ]);
        if (count($parentValList)) {
            return CustomResponse::error('请先删除子级');
        }

        $id = SysMapService::destroy(explode(',', $id));
        if ($id) {
            return CustomResponse::success($id, '删除成功');
        } else {
            return CustomResponse::error('删除失败');
        }
    }
}
